Erich Gamma와 Kent Beck에 의해 개발된 오픈 소스 프레임워크이다.
그림4.1 Test 구조(Composite Pattern)
Bradman 99.94 52 80 10 6996 334 29
Pollock 60.97 23 41 4 2256 274 7
Headly 60.83 22 40 4 2256 270* 10
Sutcliffe 60.73 54 84 9 4555 194 16
package com.oracleclub.refactoring;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class FileReaderTest extends TestCase {
private FileReader _input;
private FileReader _empty;
public FileReaderTest(String name) {
super(name);
}
protected void setUp() {
try {
_input = new FileReader("D:\\dev\\workspace\\book-refactoring\\data.txt");
_empty = newEmptyFile();
} catch(FileNotFoundException e) {
throw new RuntimeException("unable to open test file");
} catch(IOException ioe) {
throw new RuntimeException(ioe.toString());
}
}
private FileReader newEmptyFile() throws IOException {
File empty = new File("D:\\dev\\workspace\\book-refactoring\\empty.txt");
FileOutputStream out = new FileOutputStream(empty);
out.close();
return new FileReader(empty);
}
protected void tearDown() {
try {
_input.close();
} catch(IOException e) {
throw new RuntimeException("error on closing test file");
}
}
public void testRead() throws IOException {
char ch = '&';
// _input.close();
for (int i=0; i<4; i++) {
ch = (char) _input.read();
}
// assertTrue('d'==ch);
// assert('m' == ch); //에러가 발생하지 않는다.
assertEquals('d', ch); //메시지를 명확히 보여준다.
}
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(new FileReaderTest("testRead"));
suite.addTest(new FileReaderTest("testReadAtEnd"));
suite.addTest(new FileReaderTest("testReadBoundaries"));
suite.addTest(new FileReaderTest("testEmptyRead"));
suite.addTest(new FileReaderTest("testReadAfterClose"));
return suite;
}
public void testReadAtEnd() throws IOException {
int ch = -1234;
for (int i=0; i<141; i++) {
ch = _input.read();
}
assertEquals("read at end", -1, ch);
}
public void testReadBoundaries() throws IOException {
assertEquals("read first char", 'B', (char) _input.read());
int ch = 0;
for (int i=0; i<140; i++) {
ch = _input.read();
}
// assertEquals("read last char", '6', ch);
// assertEquals("read at end", -1, _input.read());
// assertEquals("read at end", -1, _input.read());
}
public void testEmptyRead() throws IOException {
assertEquals(-1, _empty.read());
}
public void testReadAfterClose() throws IOException {
_input.close();
try {
_input.read();
fail("no exception for read past end");
} catch(IOException e) { }
}
}
여기서는 눈에 띄는 차이만 간단히 살펴보고 자세한 설명은 다음으로 미루겠다.
package com.oracleclub.refactoring;
import static org.junit.Assert.assertEquals;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class FileReaderTest4 {
private FileReader _input;
@Before
public void setUp() {
try {
_input = new FileReader("D:\\dev\\workspace\\book-refactoring\\data.txt");
} catch(FileNotFoundException e) {
throw new RuntimeException("unable to open test file");
}
}
@After
public void tearDown() {
try {
_input.close();
} catch(IOException e) {
throw new RuntimeException("error on closing test file");
}
}
@Test
public void read() throws IOException {
char ch = '&';
// _input.close();
for (int i=0; i<4; i++) {
ch = (char) _input.read();
}
// assertTrue('d'==ch);
// assert('m' == ch); //에러가 발생하지 않는다.
assertEquals('d', ch); //메시지를 명확히 보여준다.
}
}